using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NTTHAO.Web.BL.Product;

public partial class admin_webparts_Plug_ins_Product_CustomProductListManagement : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string action = Request.QueryString["action"];
        string controlName = null;
        
        if (action != null)
        {
            if (action.Equals("viewlist", StringComparison.OrdinalIgnoreCase))
            {
                controlName = "CustomProductListDetail.ascx";
            }
            else if (action.Equals("adalbum", StringComparison.OrdinalIgnoreCase))
            {
                if (Request.QueryString["albumid"] != null)
                {
                    int listID = Convert.ToInt32(Request.QueryString["listid"]);
                    int detailID = Convert.ToInt32(Request.QueryString["albumid"]);
                    this.AddDetail(listID, detailID);
                    Response.Redirect("?type=" + Request.QueryString["type"] + "&name=" + Request.QueryString["name"] + "&action=viewlist&listid=" + listID.ToString(), false);
                    return;
                }
                controlName = "CustomProductListDetailInput.ascx";
            }
            else if (action.Equals("moveupalbum", StringComparison.OrdinalIgnoreCase))
            {
                try
                {
                    this.MoveUp(Int32.Parse(Request.QueryString["id"]));
                }
                catch (Exception) { }
                Response.Redirect("?type=" + Request.QueryString["type"] + "&name=" + Request.QueryString["name"] + "&action=viewlist&listid=" + Request.QueryString["listid"]);
            }
            else if (action.Equals("movealbumdown", StringComparison.OrdinalIgnoreCase))
            {
                try
                {
                    this.MoveDown(Int32.Parse(Request.QueryString["id"]));
                }
                catch (Exception) { }
                Response.Redirect("?type=" + Request.QueryString["type"] + "&name=" + Request.QueryString["name"] + "&action=viewlist&listid=" + Request.QueryString["listid"]);
            }
            else if (action.Equals("delalbum", StringComparison.OrdinalIgnoreCase))
            {
                try
                {
                    this.DeleteDetail(Int32.Parse(Request.QueryString["id"]));
                }
                catch (Exception) { }
                Response.Redirect("?type=" + Request.QueryString["type"] + "&name=" + Request.QueryString["name"] + "&action=viewlist&listid=" + Request.QueryString["listid"]);
            }
        }

        if (controlName == null)
        {
            controlName = "CustomProductListList.ascx";
        }

        if (controlName != null)
        {
            Control ctrl = LoadControl(controlName);
            this.Controls.Add(ctrl);
        }
    }

    private void MoveUp(int id)
    {
        try
        {
            CustomProductListDetail detail = new CustomProductListDetail(id);
            detail.MoveDown();
        }
        catch (Exception) { }
    }

    private void MoveDown(int id)
    {
        try
        {
            CustomProductListDetail detail = new CustomProductListDetail(id);
            detail.MoveUp();
        }
        catch (Exception) { }
    }

    private void AddDetail(int listID, int detailID)
    {
        try
        {
            CustomProductListDetail detail = new CustomProductListDetail();
            detail.ListID = listID;
            detail.ProductID = detailID;
            detail.Insert();
        }
        catch (Exception) { }
    }

    private void DeleteDetail(int id)
    {
        try
        {
            CustomProductListDetail detail = new CustomProductListDetail(id);
            detail.Delete();
        }
        catch (Exception) { }
    }
}
